﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace QPEngine
{
    public class MusicManagerHelp : MonoBehaviour
    {
        public static MusicManagerHelp Instace = new MusicManagerHelp();

        public AudioSource Audio { get; set; }

        public Hashtable Sounds = new Hashtable();

        /// <summary>
        /// 音效
        /// </summary>
        public static float soundVolume { get; set; } = 1f;
        /// <summary>
        /// 背景音乐
        /// </summary>
        public static float musicVolume { get; set; } = 1f;
        /// <summary>
        /// 禁止音效播放
        /// </summary>
        public static bool isPlaySV { get; set; } = true;
        /// <summary>
        /// 禁止背景音乐播放
        /// </summary>
        public static bool isPlayMV { get; set; } = true;


        public void Awake()
        {
            Audio = GetComponent<AudioSource>();
            if (Audio == null) { Audio = this.gameObject.AddComponent<AudioSource>(); }
        }


        public void SetValue(float sv, float mv)
        {
            soundVolume = sv;
            musicVolume = mv;
            Audio.volume = musicVolume;

        }

        /// <summary>
        /// 静音
        /// </summary>
        public void SetPlaySM(bool sv, bool mv)
        {
            isPlaySV = sv;
            isPlayMV = mv;
            Audio.mute = !isPlayMV;
        }

        public float GetSoundVolume()
        {
            return soundVolume;
        }
        public float GetMusicVolume()
        {
            return musicVolume;
        }
        public bool GetIsPlaySV()
        {
            return isPlaySV;
        }
        public bool GetIsPlayMV()
        {
            return isPlayMV;
        }

        public bool isPlaying()
        {
            return Audio.isPlaying;
        }

        /// <summary>
        /// 添加一个声音
        /// </summary>
        public void Add(string key, AudioClip value)
        {
            if (Sounds[key] != null || value == null) return;
            Sounds.Add(key, value);
        }

        /// <summary>
        /// 获取一个声音
        /// </summary>
        public AudioClip Get(string key)
        {
            if (Sounds[key] == null) return null;
            return Sounds[key] as AudioClip;
        }

        /// <summary>
        /// 载入一个音频
        /// </summary>
        public AudioClip LoadAudioClip(string path)
        {
            AudioClip ac = Get(path);
            if (ac == null)
            {
                ac = (AudioClip)Resources.Load(path, typeof(AudioClip));
                Add(path, ac);
            }
            return ac;
        }


        /// <summary>
        /// 播放背景音乐
        /// </summary>
        public void PlayBacksound(string name, bool canPlay)
        {
            if (Audio.clip != null)
            {
                if (name.IndexOf(Audio.clip.name) > -1)
                {
                    if (!canPlay)
                    {
                        Audio.Stop();
                        Audio.clip = null;
                    }
                    return;
                }
            }
            if (canPlay)
            {
                Audio.loop = true;
                Audio.clip = LoadAudioClip(name);
                Audio.volume = musicVolume;
                Audio.Play();
                Audio.mute = !isPlayMV;
            }
            else
            {
                Audio.Stop();
                Audio.clip = null;
            }
        }

        public void PlayBacksound(UnityEngine.Object clipObj, bool canPlay)
        {
            if (Audio.clip != null)
            {
                if (clipObj.name.IndexOf(Audio.clip.name) > -1)
                {
                    if (!canPlay)
                    {
                        Audio.Stop();
                        Audio.clip = null;
                    }
                    return;
                }
            }
            if (canPlay)
            {
                Audio.loop = true;
                Audio.clip = clipObj as AudioClip;
                Audio.volume = musicVolume;
                Audio.Play();
                Audio.mute = !isPlayMV;
            }
            else
            {
                Audio.Stop();
                Audio.clip = null;
            }
        }
        /// <summary>
        /// 播放音频剪辑
        /// </summary>
        public AudioSource Play(string path)
        {
            Vector3 position = Vector3.zero;
            AudioClip clip = this.LoadAudioClip(path);
            float f = isPlaySV ? soundVolume : 0;
            AudioSource source = PlayAudioClip(clip, position, f);
            return source;
        }

        public AudioSource Play(UnityEngine.Object obj)
        {
            Vector3 position = Vector3.zero;
            AudioClip clip = obj as AudioClip;
            float f = isPlaySV ? soundVolume : 0;
            AudioSource source = PlayAudioClip(clip, position, f);
            return source;
        }


        AudioSource PlayAudioClip(AudioClip clip, Vector3 position, float volume)
        {
            GameObject go = new GameObject("One shot audio");
            go.transform.position = position;
            AudioSource source = go.AddComponent<AudioSource>();
            source.clip = clip;
            source.volume = volume;
            source.spatialBlend = 0;
            source.Play();
            Destroy(go, clip.length);
            return source;
        }
    }
}